//
//  MoodContentViewLightMaker.m
//  nonghubao
//
//  Created by 王 文彬 on 13-6-6.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "MoodContentViewLightMaker.h"
#import "DynamicPicturesView.h"
#import "DynamicFooterView.h"
#import "Config.h"

@implementation MoodContentViewLightMaker

+ (void)makePrototype:(UIView *)view
{
    if (view) {
        //文字部分
        UILabel* contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 290, 20)];
        contentLabel.numberOfLines = 0;
        contentLabel.lineBreakMode = UILineBreakModeWordWrap;
        contentLabel.font = _SYS_FONT_002;
        contentLabel.backgroundColor = [UIColor clearColor];
        [view addSubview:contentLabel];
        
        
        //图片视图部分
        DynamicPicturesView *picturesView = [[DynamicPicturesView alloc] initWithFrame:CGRectMake(10, contentLabel.frame.origin.y + contentLabel.frame.size.height + 5, 280, 0)];
        picturesView.hidden = true;
        picturesView.backgroundColor = [UIColor clearColor];
        [view addSubview:picturesView];
    }
}


// 计算一个动态的高度
+ (float)heightWithDictionary:(NSDictionary *)data
{
    float height = 0.0f;
    
    if (data == nil || [data isKindOfClass:[NSNull class]]) {
        return height;
    }
    
    //文字内容
    NSString *content = data[@"content"];
    
    //根据content计算，此消息记录对应的label的size，这里的font和绘制cell时的font一定要一样
    if ([content length] != 0) {
        
        CGSize size = [content sizeWithFont:_SYS_FONT_002 constrainedToSize: CGSizeMake(280.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
        height += size.height;
    }
    
    
    //图片
    NSArray *pictures = data[@"pictures"];
    
    //动态要有图片 且 图片对象不为Null 且 图片的数量>0
    if (pictures != nil && ![pictures isKindOfClass:[NSNull class]] && pictures.count > 0) {
        
        height += _PICTURE_NORMAL_PREVIEW_HEIGHT;
    }
    
    
    //高度最后要加上间隙的高度
    height += 5 + 5 + 5 + 5 + 13;
    
    return height;
}


// 根据数据重新绘制动态内容
+ (void)drawContentView:(UIView *)view withDictionary:(NSDictionary *)data
{
    //获取文字内容
    NSString* content = data[@"content"];
    //获取文字label
    UILabel* contentLabel = view.subviews[0];
    contentLabel.text = content ? content : @"";
    
    //由于文字部分高度不定，所以需要在cell重用后向cell中加入文字部分
    //先设置动态内容部分view的高度，再设置其子view的高度，即文字说明的高度
    CGSize size = [contentLabel.text sizeWithFont:_SYS_FONT_002 constrainedToSize: CGSizeMake(280.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
    contentLabel.frame = CGRectMake(contentLabel.frame.origin.x, contentLabel.frame.origin.y, 280, size.height);
    
    
    //图片部分
    DynamicPicturesView* picturesView = view.subviews[1];
    //获取图片数据
    NSArray *pictures = data[@"pictures"];
    
    //有图片 且 图片对象不为Null 且 图片的数量>0
    if (pictures != nil && ![pictures isKindOfClass:[NSNull class]] && pictures.count > 0) {
        [picturesView drawPictures:pictures];
    }
    //否则，就传递nil对象过去，会导致图片视图隐藏
    else {
        [picturesView drawPictures:nil];
    }
    
    
    //重绘图片视图的位置
    picturesView.frame = CGRectMake(picturesView.frame.origin.x, contentLabel.frame.origin.y + contentLabel.frame.size.height + 13, picturesView.frame.size.width, picturesView.frame.size.height);
    //重绘自己视图的|高度|
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, picturesView.frame.origin.y + picturesView.frame.size.height);
}
@end
